package com.samoiloff.ui.progress {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author dngr
	 * created 29.08.2011
	 */
	public class SliderController extends EventDispatcher {
		private var _mc : MovieClip;
		
		private var _hitMc : MovieClip;
		private var _sldrMc : MovieClip;
		private var _progressMc : MovieClip;
		private var _maxProgressWidth:Number;
			
		private var _progress : Number;
		
		
		private var _min : Number;
		private var _max : Number;

		public function SliderController(mc : MovieClip) {
			this._mc = mc;
			
			_hitMc = _mc.getChildByName("hitMc") as MovieClip;
			_sldrMc = _mc.getChildByName("sldrMc") as MovieClip;
			_progressMc = _mc.getChildByName("progressMc") as MovieClip;
			
			_progressMc.mouseEnabled = false;
			_maxProgressWidth = _progressMc.width;
			
			
			_hitMc.useHandCursor = _hitMc.buttonMode = true;
			_sldrMc.useHandCursor = _sldrMc.buttonMode = true;
			
			_min = _hitMc.x;
			_max = _hitMc.x + _maxProgressWidth;
			
			if (_mc.root) {
				init();
			} else {
				_mc.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			}
			
			//_sldrMc.visible = false;
			
			progress = 0;
		}
		
		private function onAddedToStage(e : Event) : void {
			_mc.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			init();
		}
		
		private function init():void {			
			_hitMc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_sldrMc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}

		private function onMouseDown(e : MouseEvent) : void {
			_mc.root.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onMouseUp(e : MouseEvent) : void {
			_mc.root.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			_mc.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
				
		private function onEnterFrame(e : Event) : void {
			var x : Number = _mc.mouseX;
			
			x = x > _max ? _max : x;
			x = x < _min ? _min : x;
			progress = (x - _min) / _maxProgressWidth;
		}		
		
		public function get progress() : Number {
			return _progress;
		}
		
		public function set progress(progress : Number) : void {
			_progress = progress;
			_progressMc.width = _progress * _maxProgressWidth;
			_sldrMc.x = _progressMc.width + _hitMc.x;

//			trace("SliderController.progress (progress ) : " + _progress);
			dispatchEvent(new Event(Event.CHANGE));
		}

		public function destroy():void {
			_mc.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			_hitMc.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_sldrMc.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_mc.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			if (_mc.root) {
				_mc.root.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			}
			_mc = null;
			_hitMc = null;
			_sldrMc = null;
		}
	}
}
